<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Sam and Max TTY</title>
		<style type="text/css">
			body{ padding:0px; margin:0px; background:#000; color:#fff; font-family:monospace; font-size:14px;}
			#screen{white-space:pre-wrap;}
			#submit{display:block;}
			#prompt{display:block; width:100%; border:none; outline:none; background:inherit; color:inherit; font-family:inherit; font-size:inherit; border-bottom:2px solid #fff;}
		</style>
		<script type="application/javascript;version=1.8" src="data.js"></script>
		<script type="application/javascript;version=1.8"><![CDATA[
		
			var elScreen, elPrompt, elSubmit;
			window.onload=function(){
				elScreen=document.getElementById("screen");
				elPrompt=document.getElementById("prompt");
				elSubmit=document.getElementById("submit");
				elSubmit.onsubmit=function(){ echo("=="+elPrompt.value+"=="); onEnter(decodeString(elPrompt.value)); elPrompt.value=""; window.scrollTo(0,document.body.offsetHeight); elPrompt.focus(); return false;};
				window.onclick=function(){ elPrompt.focus(); };
				buildWildcardActions();
				
				if(!restore())
					action("!startup");
				elPrompt.focus();
			};
			
			function echo(txt){
				if(arguments.length>1)
					txt=Array.prototype.slice.call(arguments).join("\n")
				
				elScreen.appendChild(document.createTextNode(txt+"\n"));
				
				if(!(localStorage["-screen-"]))
					localStorage["-screen-"]="";
					
				localStorage["-screen-"]+=(txt+"\n");
			}
			
			function echor(){
				echo(arguments[Math.floor(Math.random()*arguments.length)]);
			}
			
			function setState(k,v){
				if(typeof(v)!="undefined")
					state[k]=v;
				localStorage["-state-"+k]=JSON.stringify(state[k]);
			}
			
			
			function restore(){
				if(!localStorage || !(localStorage["-screen-"]))
					return false;
				
				elScreen.textContent=localStorage["-screen-"];
				for(var i=0;i<localStorage.length;i++){
					var m;
					if(m=(/^\-state\-(.*)$/g).exec(localStorage[i])){
						state[m[1]]=JSON.parse(localStorage[localStorage[i]]);
					}
				}
				return true;
			};
			
			function reset(){
				localStorage.clear();
				document.location.reload();
			}
			
			function decodeString(txt){
				var a=txt.replace(/[^a-z0-9]/gi," ").replace(/(?:^\s+|\s+$)/g,"").replace(/\s+/g," ").split(" ");
				for(var i=0;i<a.length-1;i++)
					if(dictionary[(a[i]+" "+a[i+1]).toLowerCase()]){
						a[i]=dictionary[(a[i]+" "+a[i+1]).toLowerCase()];
						a[i+1]="";
					}
				
				for(var i=0;i<a.length;i++)
					if(a[i] && dictionary[a[i].toLowerCase()])
						a[i]=dictionary[a[i].toLowerCase()];
				
				return a.filter(function(el){ return !!el; }).join(" ");
			}
			
			function runWildcard(target,args){
				if(typeof(target)=="function"){
					target(args);
					return true;
				}else if(typeof(target)=="string"){
					if(target[0]=="!")
						return action(target);
					else if(target[0]=="~")
						return action(target.replace(/^\~/g,""));
					else{
						for(var i in args)
							target=target.replace("%"+i+"%",args[i]);
						echo(target);
						return true;
					}
				}else if(target["!"]){
					return runWildcard(target["!"],args);
				}else{
					return false;
				}
			}
			
			function actionLiteral(actions,txt){
				if(!actions)
					return false;
				else if(typeof(actions[txt.toLowerCase()])=="function"){
					actions[txt.toLowerCase()]();
					return true;
				}else if(typeof(actions[txt.toLowerCase()])=="string"){
					var target=(actions[txt.toLowerCase()]);
					if(target[0]=="!")
						return action(target);
					else if(target[0]=="~")
						return action(target.replace(/^\~/g,""));
					else{
						echo(target);
						return true;
					}
				}
				return false;
			}
			
			function actionWildcard(wildcardActions,txt){
				if(!wildcardActions)
					return false;
				
				txt=txt.split(" ");
				var lvl=wildcardActions;
				var args=[];
				
				for(var i=0;i<txt.length;i++){
					var last=(i==(txt.length-1));
					
					if(lvl[txt[i].toLowerCase()]){
						if(last)
							return runWildcard(lvl[txt[i].toLowerCase()],args);
						else 
							lvl=lvl[txt[i].toLowerCase()];
					}else if(lvl["*"]){
						args.push(txt[i]);
						if(last)
							return runWildcard(lvl["*"],args);
						else
							lvl=lvl["*"];
					}else return false;
					
				}
				
				return false;
			}
			
			function action(txt,unfiltered){
				if( actionLiteral(actions["room-"+state.room],txt) || actionLiteral(actions["global"],txt) )
					return true;
				else if( actionWildcard(wildcardActions["room-"+state.room],txt) || actionWildcard(wildcardActions["global"],txt) )
					return true;
				else
					return false;
			}
			
			var wildcardActions={};
			
			function buildWildcardActionsPerRoom(actions,wildcardActions){
				for(var i in actions){	
					if(i.indexOf("*")>=0){
						var l=i.split(" ");
						var base=wildcardActions;
						for(var j=0;j<l.length-1;j++){
							if(typeof(base[l[j]])=="undefined"){
								base=base[l[j]]={};
							}else if(typeof(base[l[j]])=="string" || typeof(base[l[j]])=="function"){
								base=base[l[j]]={"!":base[l[j]]};
							}else if(typeof(base[l[j]])=="object"){
								base=base[l[j]];
							}
						}
						var last=l.pop();
						if(typeof(base[last])=="object")
							base[last]["!"]=actions[i];
						else
							base[last]=actions[i];
					}	
				}
			}
			
			function buildWildcardActions(){
				for(var i in actions){
					wildcardActions[i]={};
					buildWildcardActionsPerRoom(actions[i],wildcardActions[i]);
				}
			}
			
			function onEnter(txt){
				if(!action(txt))
					action("!not unerstood");
			}
		]]></script>
	</head>
	<body>
		<pre id="screen"></pre>
		<form id="submit" autocomplete = "off">
			<input id="prompt" type="text" value="" />
			<input type="submit" style="display:none" value="Enter" />
		</form>
	</body>
</html>